package model.dao;

import java.sql.ResultSet;
import java.util.ArrayList;

import controller.util.ControllerHelper;

import model.pojo.*;
import model.util.*;



public class LoaiSanPhamDAO {
	//lay danh sách loại sản phẩm
	public static ArrayList<LoaiSanPham> LayDSLoaiSanPham()
	{
		ArrayList<LoaiSanPham> arr = new ArrayList<LoaiSanPham>();
		
		DataHelper helper = new DataHelper();
        try
        {
            helper.open();
            ArrayList<Object> para = new ArrayList<Object>();
            ResultSet result = helper.executeProcedure("{call sp_selectAllLoaiSanPham()}", para);
            while(result.next())
            {
                LoaiSanPham lsp = new LoaiSanPham();                
                lsp.set_maLoai(result.getInt("MaLoai"));
                lsp.set_tenLoaiSanPham(result.getString("TenLoaiSanPham"));
                arr.add(lsp);
            }
            
        }catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        finally
        {
            helper.close();
        }
  
		return arr;
	}
	//thêm loại SP
	public static boolean ThemLoaiSanPham(LoaiSanPham lsp)
	{
		boolean kq = false;
		DataHelper helper = new DataHelper();
        try
        {
            helper.open();
            ArrayList<Object> para = new ArrayList<Object>();
            para.add(lsp.get_tenLoaiSanPham());
            String sp = ControllerHelper.getStoreProcedureString("ThemLoaiSP");
            int kqIns = helper.executeInsProcedure(sp, para);
            if(kqIns>0)
            {
            	kq=true;
            }
            
        }catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        finally
        {
            helper.close();
        }
		return kq;
	}
	//kiêm tra loai san pham theo ten KiemTraLoaiSP=
	public static ArrayList<LoaiSanPham> KiemTraLoaiSanPham(String tenLoai)
	{
		ArrayList<LoaiSanPham> arr = new ArrayList<LoaiSanPham>();
		
		DataHelper helper = new DataHelper();
        try
        {
            helper.open();
            ArrayList<Object> para = new ArrayList<Object>();
            para.add(tenLoai);
            String sp = ControllerHelper.getStoreProcedureString("KiemTraLoaiSP");
            ResultSet result = helper.executeProcedure(sp, para);
            while(result.next())
            {
                LoaiSanPham lsp = new LoaiSanPham();                
                lsp.set_maLoai(result.getInt("MaLoai"));
                lsp.set_tenLoaiSanPham(result.getString("TenLoaiSanPham"));
                arr.add(lsp);
            }
            
        }catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        finally
        {
            helper.close();
        }
  
		return arr;
	}
}
